/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Guis.Principal;

import BD.CargarConfig;
import BD.ConfiguracionMySql;
import BD.MyException;
import BD.MySql;
import BD.MySqlCreador;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author angelo
 */
public class Config_DB extends javax.swing.JDialog {

    /**
     * Creates new form Config_DB
     */
//    private final String nombre = "TSB";//nombre de la base de datos.
    private final String nombre = "sample";//nombre de la base de datos.  es el nombre de la bd en derby

    public Config_DB(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.setResizable(false);
        this.setTitle("Configurar Base De Datos");

        Dimension desktopSize = parent.getSize();
        Dimension jInternalFrameSize = this.getSize();
        this.setLocation((desktopSize.width - jInternalFrameSize.width) / 2,
                (desktopSize.height - jInternalFrameSize.height) / 2);
//******************************************************************************
        this.jButton1.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    metodoSalir();
                }

            }
        });
        this.jButton2.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    metodoMostrar();
                }


            }
        });
        this.jButton3.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    metodoCargar();
                }

            }
        });
        this.jButton4.addKeyListener(new java.awt.event.KeyAdapter() {

            @Override
            public void keyPressed(java.awt.event.KeyEvent e) {
                if (KeyEvent.VK_ENTER == e.getKeyCode()) {
                    metodoCrearTablas();
                }

            }
        });

    }

    private void metodoSalir() {
        this.dispose();
    }

    private void metodoMostrar() {

        this.jLabel1.setForeground(Color.black);
        this.jLabel2.setForeground(Color.black);
        this.jLabel4.setForeground(Color.black);

        ConfiguracionMySql cfg2;
        CargarConfig cargarCfg = new CargarConfig();
        cfg2 = cargarCfg.LeerArchivo();

        if (cfg2 == null) {
            JOptionPane.showMessageDialog(this, "Ninguna configuracion guardada.");
        } else {
            this.jTextField1.setText(cfg2.getUser());
            this.jPasswordField1.setText(cfg2.getPw());
            this.jTextField4.setText(cfg2.getHost());
            System.out.println(cfg2.getDataBaseName());
        }
    }

    private void metodoCargar() {
        if (this.Validar()) {

            CargarConfig cargarCfg = new CargarConfig();

            ConfiguracionMySql cfg = new ConfiguracionMySql(this.jTextField1.getText(),
                    this.jPasswordField1.getText(), this.nombre, this.jTextField4.getText());

            if (cargarCfg.CrearArchivo(cfg)) {
                JOptionPane.showMessageDialog(this, "Configuracion Guardada");
                MySql.cargarArchivo();
            } else {
                JOptionPane.showMessageDialog(this, "Error al guardar configuracion");
            }
        }
    }

    private void metodoCrearTablas() {
        ConfiguracionMySql cfg2;
        CargarConfig cargarCfg = new CargarConfig();
        cfg2 = cargarCfg.LeerArchivo();

        if (cfg2 == null) {
            JOptionPane.showMessageDialog(this, "Ninguna configuracion guardada.");
        } else {
            try {
                MySqlCreador sqlC = new MySqlCreador(cfg2.getUser(), cfg2.getPw(), cfg2.getHost(), nombre);
                sqlC.Conectar();
//                int res = sqlC.CrearBD();
//
//                switch (res) {
//                    case -1:
//                        JOptionPane.showMessageDialog(this, "Error al crear base de datos: Base de datos EXISTE.");
//                        break;
//
//                    case 0:
//                        JOptionPane.showMessageDialog(this, "Error al crear base de datos.");
//                        break;
//
//                    case 1:
//                        JOptionPane.showMessageDialog(this, "Base de datos CREADA.");
//                        break;
//                }
                int res = sqlC.CrearTablas();
                switch (res) {
                    case -1:
                        JOptionPane.showMessageDialog(this, "Error al crear tablas: Tablas EXISTENTES.");
                        break;

                    case 0:
                        JOptionPane.showMessageDialog(this, "Error al crear tablas");
                        break;

                    case 1:
                        JOptionPane.showMessageDialog(this, "Tablas creadas.");
                        break;
                }
                sqlC.Desconectar();
            } catch (MyException e) {
                JOptionPane.showMessageDialog(rootPane, e.getMessage());
            }
        }
    }

    private boolean Validar() {
        boolean flag = true;

        if (this.jTextField1.getText().length() == 0) {
            this.jLabel1.setForeground(Color.red);
            this.jTextField1.grabFocus();
            flag = false;
        } else {
            this.jLabel1.setForeground(Color.black);
        }

        if (this.jPasswordField1.getText().length() == 0) {
            this.jLabel2.setForeground(Color.red);
            this.jPasswordField1.grabFocus();
            flag = false;
        } else {
            this.jLabel2.setForeground(Color.black);
        }

        if (this.jTextField4.getText().length() == 0) {
            this.jLabel4.setForeground(Color.red);
            this.jTextField4.grabFocus();
            flag = false;
        } else {
            this.jLabel4.setForeground(Color.black);
        }

        return flag;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel2.setText("*Clave");

        jLabel6.setText("*Para crear la DB y las tablas primero debe cargar la configuracion.");

        jLabel1.setText("*Usuario");

        jLabel4.setText("*Host");

        jButton1.setText("Salir");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Mostrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Cargar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Crear DB y tablas");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                            .addComponent(jTextField4)
                            .addComponent(jPasswordField1))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jButton4)
                .addGap(44, 44, 44)
                .addComponent(jButton2)
                .addGap(45, 45, 45)
                .addComponent(jButton3)
                .addGap(68, 68, 68)
                .addComponent(jButton1)
                .addContainerGap(70, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 21, Short.MAX_VALUE)
                .addComponent(jLabel6)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.metodoSalir();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.metodoMostrar();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        this.metodoCargar();

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        this.metodoCrearTablas();
    }//GEN-LAST:event_jButton4ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration//GEN-END:variables
}
